import {swfobject} from '../../common/swfobject'
import swf from './swf/xchat_v1.swf'
import EventEmitter from '../../common/EventEmitter'
import {chatEvents} from '../chatEvents'
import {xchat_swf_message} from './xchat_swf_message'
import {xchat_swf_error} from './xchat_swf_error'
export default class XchatSwf{
	constructor(swf_param,div_id = 'xchat',callbackFn = null) {
	  this.swf_param = swf_param
		if(div_id){
			this.div_id = div_id
		}else{
			console.error('初始化flashSocket的flashSocketWrapId参数必传!')
		}
		this.callbackFn = callbackFn
		this.init()
	}
	init(){
		this.emitter = new EventEmitter()
		xchat_swf_message.emitter = xchat_swf_error.emitter = this.emitter
		xchat_swf_message.chatEvents = xchat_swf_error.chatEvents =chatEvents
		window.xchat_swf_message = xchat_swf_message
		if (this.callbackFn == null){
			this.callbackFn = this.Created_cb;
		}
		var swfVersionStr = "11.0.0";
		var xiSwfUrlStr = "playerProductInstall.swf";
		var flashvars = {};
		flashvars = {
			ip:this.swf_param.ip,
			port:this.swf_param.port,
			gid:this.swf_param.room_id,
			uid:this.swf_param.uid,
			level:this.swf_param.level,
			token:this.swf_param.token,
			appdata:this.swf_param.appdata
		};
		var params = {};
		params.quality = "high";
		params.bgcolor = "#57799c";
		params.allowscriptaccess = "always";
		params.allowfullscreen = "true";
		params.wmode = "transparent";
		var attributes = {};
		attributes.id = this.div_id;
		attributes.name = this.div_id;
		attributes.align = "middle";
		swfobject.embedSWF(swf,this.div_id,
			document.getElementById(this.div_id).offsetWidth, document.getElementById(this.div_id).offsetWidth, 
			swfVersionStr, xiSwfUrlStr, 
			flashvars, params, attributes,this.callbackFn);
	}
	Created_cb(cbobj){
		if (cbobj.success){
		}
		else{
			alert('flash初始化失败，请安装插件，如果您确定已经有flash插件，请更换浏览器尝试或者联系客服')
		}
	}
	swf(){
		return swfobject.getObjectById("xchat")
	}
	send(to_uid,data){
		this.swf().Chat(to_uid,data)
	}
	disablechat(touid,mins){
		this.swf().DisableChat(touid,mins)
	}
	kickout(touid){
		this.swf().Kickout(touid);
	}
	tokenop(type,TO_UID,comment){
		if (type=='0'){
			this.swf().TokenOp(0,TO_UID,comment)
		}
		if (type=='1'){
			this.swf().TokenOp(1,TO_UID,comment)
			var currentdate = new Date() 
			var datetime = currentdate.getHours() + ":" + currentdate.getMinutes()
		}

	}
	tokenadjust(TO_UID,index){
		this.swf().TokenAdjust(TO_UID,index)
	}
	on(event, listener) {
		this.emitter.on(event, listener)
	}
	off(event, listener) {
		this.emitter.off(event, listener)
	}
}